﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Job.Entity;
using Job.Data;
using Job.Data.Interfaces;
using Job.Web.Framework;
using System.Data;

namespace Job.Logic.Common
{
    public class DictionaryLogic
    {
        public bool InsertCommon(JobCommonDictionary dict)
        {
            try
            {
                DataAccess.commonDictDao.Insert(dict);
            }
            catch
            {
                return false;
            }
            return true;
        }

        public bool UpdateCommon(JobCommonDictionary dict)
        {
            try
            {
                DataAccess.commonDictDao.Update(dict);
            }
            catch
            {
                return false;
            }
            return true;
        }

        public JobCommonDictionary SelectCommon(int id)
        {            
            return DataAccess.commonDictDao.Find(id);
        }

        public void DeleteCommon(int id)
        {
            JobCommonDictionary dict = new JobCommonDictionary();
            dict.DictId = id;
            DataAccess.commonDictDao.Delete(dict);
        }


        public int CountCommon(string type)
        {
            return DataAccess.commonDictDao.GetCountByClass(type);
        }
        public int CountCommon(Constants.DictionaryType type)
        {
            string dictClass = Constants.GetDictionaryCode(type);
            return CountCommon(dictClass);
        }

        public int CountActiveCommon(string type)
        {
            return DataAccess.commonDictDao.GetActiveCountByClass(type);
        }
        public int CountActiveCommon(Constants.DictionaryType type)
        {
            string dictClass = Constants.GetDictionaryCode(type);
            return CountActiveCommon(dictClass);
        }

        public IList<JobCommonDictionary> GetAll()
        {
            return DataAccess.commonDictDao.FindAll();
        }
		

        public DataTable GetCommonPageList(string type, int pageSize, int pageIndex)
        {
            string sql = @"Select dict_id,dict_class,dict_name,dict_state "
                + "From job_common_dictionary Where dict_class='" + type + "'";
            CommonLogic common = new CommonLogic();
            DataSet ds = common.GetPageDataBySql(sql, pageSize, pageIndex, 0,
                "dict_id", SortDirection.Ascending, null);
            return ds.Tables[0];
        }

        public DataTable GetCommonPageList(Constants.DictionaryType type, int pageSize, int pageIndex)
        {
            string dictClass = Constants.GetDictionaryCode(type);
            return GetCommonPageList(dictClass, pageSize, pageIndex);
        }

        public IList<JobCommonDictionary> GetDictByClass(string type)
        {
            return DataAccess.commonDictDao.FindByDictClass(type);
        }

        public IList<JobCommonDictionary> ListActiveCommonByClass(Constants.DictionaryType type)
        {
            return ListActiveCommonByClass(Constants.GetDictionaryCode(type));
        }

        public IList<JobCommonDictionary> ListActiveCommonByClass(string type)
        {
            return DataAccess.commonDictDao.FindActiveByDictClass(type);
        }

        public bool InsertLevel(JobLevelDictionary dict)
        {
            try
            {
                DataAccess.levelDictDao.Insert(dict);
            }
            catch
            {
                return false;
            }
            return true;
        }

        public bool UpdateLevel(JobLevelDictionary dict)
        {
            try
            {
                DataAccess.levelDictDao.Update(dict);
            }
            catch
            {
                return false;
            }
            return true;
        }

        public JobLevelDictionary SelectLevel(int id)
        {
            return DataAccess.levelDictDao.Find(id);
        }

        public void DeleteLevel(int id)
        {
            JobLevelDictionary dict = new JobLevelDictionary();
            dict.DictId = id;
            DataAccess.levelDictDao.Delete(dict);
        }

        
        public JobLevelDictionary SelectLevelByFatherAndClass(string father, string type)
        {
            IList<JobLevelDictionary> list = ListActiveSonByFatherAndClass(type, father);
            if (list == null || list.Count == 0)
                return null;
            else
                return list[0];
        }

        public int CountFatherByClass(Constants.DictionaryType type)
        {
            string str = Constants.GetDictionaryCode(type);
            return CountFatherByClass(str);
        }
        public int CountFatherByClass(string type)
        {
            return DataAccess.levelDictDao.GetCountByFatherAndClass(Constants.EMPTY_FATHER, type);
        }

        public int CountSonByFatherAndClass(Constants.DictionaryType type, string father)
        {
            string str = Constants.GetDictionaryCode(type);
            return CountSonByFatherAndClass(str, father);
        }
        public int CountSonByFatherAndClass(string type, string father)
        {
            return DataAccess.levelDictDao.GetCountByFatherAndClass(father, type);
        }

        public IList<JobLevelDictionary> ListActiveFatherByClass(Constants.DictionaryType type)
        {
            string str = Constants.GetDictionaryCode(type);
            return ListActiveFatherByClass(str);
        }
        public IList<JobLevelDictionary> ListActiveFatherByClass(string type)
        {
            return DataAccess.levelDictDao.ListActiveByFatherAndClass(Constants.EMPTY_FATHER, type);
        }


        public IList<JobLevelDictionary> ListActiveSonByFatherAndClass(Constants.DictionaryType type, string father)
        {
            string str = Constants.GetDictionaryCode(type);
            return ListActiveSonByFatherAndClass(type, father);
        }
        public IList<JobLevelDictionary> ListActiveSonByFatherAndClass(string type, string father)
        {
            return DataAccess.levelDictDao.ListActiveByFatherAndClass(father, type);
        }

        public IList<JobLevelDictionary> ListActiveBysubCodeAndClass(string subCode, string dictClass)
        {
            return DataAccess.levelDictDao.ListActiveBySubCodeAndClass(subCode, dictClass);
        }
        public IList<JobLevelDictionary> ListBySubCodeAndClass(string subCode, string dictClass)
        {
            return DataAccess.levelDictDao.ListBySubCodeAndClass(subCode, dictClass);
        }

        public DataTable GetLevelPageList(string type,string father, int pageSize, int pageIndex)
        {
            string sql = @"Select dict_id,sub_code,dict_name,dict_state "
                + "From job_level_dictionary Where dict_class='" + type + "' "
                + "And father_code='" + father + "'";
            CommonLogic common = new CommonLogic();
            DataSet ds = common.GetPageDataBySql(sql, pageSize, pageIndex, 0,
                "dict_id", SortDirection.Ascending, null);
            return ds.Tables[0];
        }

        public DataTable GetLevelPageList(Constants.DictionaryType type, string father,int pageSize, int pageIndex)
        {
            string dictClass = Constants.GetDictionaryCode(type);
            return GetLevelPageList(dictClass, father, pageSize, pageIndex);
        }

        public DataSet FetchLevelDictData(string dictClass)
        {
            CommonLogic common = new CommonLogic();
            DataSet ds = common.FetchLevelDictData(dictClass);
            return ds;
        }

        public IList<JobLevelDictionary> FindAllByClass(string dictClass)
        {
            return DataAccess.levelDictDao.FindByDictClass(dictClass);
        }

    }
}
